<?php 

class Event_ProductForm extends Crud_Event_Handler 
{
	protected $_type = 'form';
		
	/**
	 * Run handler with options array.
	 * 
	 * @param Crud_Form $form
	 * @return void
	 */
	public function run($form)
	{
		// Синоним не визивиг
		$element = $form->getElement('synonims');
		$element->class = null;
		
		// Внутренний коммент не визивиг
		$element = $form->getElement('internal_comment');
		$element->class = null;
		
		// Сео текст не визивиг
		$element = $form->getElement('additional_text');
		$element->class = null;

		$element = $form->getElement('age_from');
		$element->setRequired(0);

		$element = $form->getElement('age_to');
		$element->setRequired(0);

		$element = $form->getElement('artikul');
		$element->setRequired(0);
/*		$element = $form->getElement('weight');
		$value = $element->getValue();
		if ($value) {
			$round = round($value/1000, 3);
			$element->setValue($round);
		}*/
		
		
		$this->_attributes($form);
		$this->_photo($form);
		$this->_alike($form);
		
		$decoratorScript = new Zend_Form_Decorator_ViewScript();
		$decoratorScript->setViewScript('product/form.phtml');
	    $form->setDecorators(array(
	        new Zend_Form_Decorator_PrepareElements(),
	    	$decoratorScript,
	    ));
	}
	
	/**
	 * Добавляет в форму элементы для редактирования все аттрибутов.
	 * 
	 * @param Crud_Form $form
	 * @return void
	 */
	protected function _attributes($form)
	{
		$product = $form->getRow();
		$peer = Product2productAttributePeer::getInstance();

		$attributes = ProductAttributePeer::getInstance()->fetchAll();
		foreach ($attributes as $attribute) {
			$value = null;
			if ($product) {
				$select = $peer->select();
				$select->where(Product2productAttributePeer::PRODUCT_ID, $product->getId());
				$select->where(Product2productAttributePeer::ATTRIBUTE_ID, $attribute->getId());
				$p2a = $peer->fetchRow($select);
				
				if ($p2a) {
					$value = $p2a->getAttributeValue();
				}
			}
			
			if ($attribute->getType() == 'file') {
				$e = new Crud_ZendFormElement_SimpleFile('file_' . $attribute->getKey());
				$e->setValue($value);
			} else {
				$e = new Zend_Form_Element($attribute->getKey());
				$e->setBelongsTo('attribute');
			}
			
			$e->setLabel('Дополнительно: ' . $attribute->getName());
			$e->setAttrib('default', $value);
			$form->addElement($e);
		}
	}
	
	protected function _photo($form)
	{
		$e = new Crud_ZendFormElement_SimpleFile('photo_1');
		$e->setLabel('Добавить фото ');
		
		$decoratorScript = new Zend_Form_Decorator_ViewScript();
		$decoratorScript->setViewScript('product/element_photo.phtml');
	    $e->setDecorators(array(
	        new Zend_Form_Decorator_PrepareElements(),
	    	$decoratorScript,
	    ));
		    
		$form->addElement($e);
		
	}
	
	protected function _alike($form)
	{
		$e = new Zend_Form_Element('alike');
		
		$decoratorScript = new Zend_Form_Decorator_ViewScript();
		$decoratorScript->setViewScript('product/element_alike.phtml');
	    $e->setDecorators(array(
	        new Zend_Form_Decorator_PrepareElements(),
	    	$decoratorScript,
	    ));
	    
		$form->addElement($e);
		
		$e = new Zend_Form_Element('attendant');
		
		$decoratorScript = new Zend_Form_Decorator_ViewScript();
		$decoratorScript->setViewScript('product/element_attendant.phtml');
	    $e->setDecorators(array(
	        new Zend_Form_Decorator_PrepareElements(),
	    	$decoratorScript,
	    ));
	    
		$form->addElement($e);

		$e = new Zend_Form_Element('attendant_category');

		$decoratorScript = new Zend_Form_Decorator_ViewScript();
		$decoratorScript->setViewScript('product/element_attendant_category.phtml');
	    $e->setDecorators(array(
	        new Zend_Form_Decorator_PrepareElements(),
	    	$decoratorScript,
	    ));

		$form->addElement($e);
	}
}
